package com.cskaoyan.market.service.impl;

import com.cskaoyan.market.db.domain.MarketAd;
import com.cskaoyan.market.db.domain.MarketAdExample;
import com.cskaoyan.market.db.domain.MarketTopic;
import com.cskaoyan.market.db.domain.MarketTopicExample;
import com.cskaoyan.market.db.mapper.MarketAdMapper;
import com.cskaoyan.market.db.mapper.MarketTopicMapper;
import com.cskaoyan.market.service.AdminTopicService;
import com.cskaoyan.market.util.MybatisUtil;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author Meltedicecream
 * @since 2023/06/21 20:30
 */
public class AdminTopicServiceImpl implements AdminTopicService {
    @Override
    public List<MarketTopic> list(Integer page, Integer limit, String title, String subtitle, String sort, String order) {
        SqlSession session = MybatisUtil.getSession();
        MarketTopicMapper mapper = session.getMapper(MarketTopicMapper.class);
        MarketTopicExample marketTopicExample = new MarketTopicExample();
        MarketTopicExample.Criteria criteria = marketTopicExample.createCriteria();
        if (!StringUtils.isEmpty(title)) {
            criteria.andTitleLike("%" + title + "%");
        }
        if (!StringUtils.isEmpty(subtitle)){
            criteria.andSubtitleLike("%" + subtitle + "%");
        }

        marketTopicExample.setOrderByClause(sort + " " + order);

        PageHelper.startPage(page,limit);

        List<MarketTopic> marketTopics = mapper.selectByExample(marketTopicExample);
        session.commit();
        session.close();
        return marketTopics;
    }

    @Override
    public MarketTopic create(String title, String subtitle, BigDecimal price, String readCount, String picUrl, Integer[] goods, String content) {
        SqlSession session = MybatisUtil.getSession();
        MarketTopicMapper marketTopicMapper = session.getMapper(MarketTopicMapper.class);
        MarketTopic marketTopic = new MarketTopic();
        marketTopic.setTitle(title);
        marketTopic.setSubtitle(subtitle);
        marketTopic.setPrice(price);
        marketTopic.setReadCount(readCount);
        marketTopic.setPicUrl(picUrl);
        marketTopic.setGoods(goods);
        marketTopic.setContent(content);
        marketTopic.setAddTime(LocalDateTime.now());
        marketTopic.setUpdateTime(LocalDateTime.now());
        marketTopicMapper.insert(marketTopic);
        session.commit();
        session.close();
        return marketTopic;
    }

    @Override
    public void delete(Integer id) {
        SqlSession session = MybatisUtil.getSession();
        MarketTopicMapper marketTopicMapper = session.getMapper(MarketTopicMapper.class);
        marketTopicMapper.deleteByPrimaryKey(id);
        session.commit();
        session.close();
    }

    @Override
    public MarketTopic update(Integer id, String title, String subtitle, BigDecimal price, String readCount, String picUrl, Integer[] goods, String content, Integer sortOrder, LocalDateTime addTime, LocalDateTime updateTime) {
        SqlSession session = MybatisUtil.getSession();
        MarketTopicMapper marketTopicMapper = session.getMapper(MarketTopicMapper.class);
        MarketTopic marketTopic = new MarketTopic();
        marketTopic.setId(id);
        marketTopic.setTitle(title);
        marketTopic.setSubtitle(subtitle);
        marketTopic.setPrice(price);
        marketTopic.setReadCount(readCount);
        marketTopic.setPicUrl(picUrl);
        marketTopic.setGoods(goods);
        marketTopic.setContent(content);
        marketTopic.setSortOrder(sortOrder);
        marketTopic.setAddTime(addTime);
        marketTopic.setUpdateTime(updateTime);
        marketTopicMapper.updateByPrimaryKey(marketTopic);
        session.commit();
        session.close();
        return marketTopic;


    }

    @Override
    public MarketTopic read(Integer id) {
        SqlSession session = MybatisUtil.getSession();
        MarketTopicMapper marketTopicMapper = session.getMapper(MarketTopicMapper.class);
        MarketTopic marketTopic = marketTopicMapper.selectByPrimaryKey(id);
        return marketTopic;
    }


}
